package com.vhuomai.store.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.vhuomai.store.constant.StoreSolution;
import com.vhuomai.store.entity.BaseStoreConfig;
import com.vhuomai.store.exception.StoreException;
import com.vhuomai.store.provider.CosStoreProvider;
import com.vhuomai.store.provider.LocalStoreProvider;
import com.vhuomai.store.provider.QiniuStoreProvider;
import com.vhuomai.store.provider.StoreProvider;
import com.vhuomai.store.service.StoreConfigService;
import com.vhuomai.store.service.StoreService;
import org.apache.tika.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Service
public class StoreServiceImpl implements StoreService {
    
    @Autowired
    private StoreConfigService configService;
    
    private Map<StoreSolution, StoreProvider> providerMap = new HashMap<>();
    
    public StoreServiceImpl() {
        providerMap.put(StoreSolution.LOCAL, new LocalStoreProvider());
        providerMap.put(StoreSolution.TENCENT, new CosStoreProvider());
        providerMap.put(StoreSolution.QINIU, new QiniuStoreProvider());
    }
    
    @Override
    public String saveFile(byte[] content, String ext) {
        BaseStoreConfig storeConfig = this.getConfig();
        return providerMap.get(storeConfig.getSolution()).saveFile(storeConfig, content, ext);
    }
    
    @Override
    public String saveFile(byte[] content) {
        return this.saveFile(content, "");
    }
    
    @Override
    public String saveFileByName(byte[] content, String fileName) {
        BaseStoreConfig storeConfig = this.getConfig();
        return providerMap.get(storeConfig.getSolution()).saveFile(storeConfig, content, fileName);
    }
    
    @Override
    public String saveFile(InputStream stream) {
        return this.saveFile(stream, "");
    }
    
    @Override
    public String saveFile(InputStream stream, String ext) {
        try {
            return saveFile(IOUtils.toByteArray(stream), ext);
        } catch (IOException e) {
            throw new StoreException("数据转换失败");
        }
    }
    
    @Override
    public String saveFileByName(InputStream stream, String fileName) {
        try {
            return saveFileByName(IOUtils.toByteArray(stream), fileName);
        } catch (IOException e) {
            throw new StoreException("数据转换失败");
        }
    }
    
    @Override
    public boolean fileExist(String url) {
        BaseStoreConfig storeConfig = this.getConfig();
        return providerMap.get(storeConfig.getSolution()).fileExist(storeConfig, url);
    }
    
    @Override
    public void remove(String url) {
        BaseStoreConfig storeConfig = this.getConfig();
        providerMap.get(storeConfig.getSolution()).remove(storeConfig, url);
    }
    
    @Override
    public InputStream getContent(String url) {
        BaseStoreConfig storeConfig = this.getConfig();
        return providerMap.get(storeConfig.getSolution()).getContent(storeConfig, url);
    }
    
    @Override
    public String toPublish(String fileName) {
        BaseStoreConfig storeConfig = this.getConfig();
        return storeConfig.getDomainUrl() + "/" + storeConfig.getMerchantNo() + "/" + fileName;
    }
    
    private BaseStoreConfig getConfig() {
        BaseStoreConfig storeConfig = configService.find();
        AssertUtils.notNull(storeConfig, "缺失存储配置");
        return storeConfig;
    }
}
